<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


include("sqlconnect.php");
include("sqlgenerator.php");
$debug = false;

$access = $_POST['access'];
$username = $_POST['username'];
$password = $_POST['password'];

switch (strtolower($access)) {
    case 'kteam':
        $db_table = 'kteam';
        break;
    case 'producer':
        $db_table = 'producer';
        break;
    case 'agent':
        $db_table = 'agent';
        break;
    case 'customer':
        $db_table = 'customer';
        break;
    default:
        $db_table = 'kteam';
        break;

}

db_connect();

$query = login_user($db_table, $username, $password);
if ($debug)
    echo 'Query: ' . $query;
$result = db_query($query);

if ($debug)
    echo 'Query Result: ' .$resut;


if (mysql_num_rows($result) != 0)
{
    session_start();
    $session_auth = 'customer_auth';
    $redirect = 'customer_edit.html';
    switch($db_table) {
        case 'kteam':
            $session_auth = 'kteam_auth';
            $redirect = 'producer.html';
            break;
        case 'producer':
            $session_auth = 'producer_auth';
            $redirect = 'producer.html';
            break;
        case 'agent':
            $session_auth = 'agent_auth';
            $redirect = 'customer.html';
            break;
        case 'customer':
            $session_auth = 'customer_auth';
            $redirect = 'ticketstatus.html';
            break;

    }
    session_register($session_auth);
    $_SESSION[$session_auth] = true;

    // Store the username who has logged in
    $expire=time()+60*60;
    setcookie("username", $username, $expire);
    setcookie("session_auth", $session_auth, $expire);

    //echo 'SESSION kteam: ' . $_SESSION['kteam_auth'] . ' producer:' . $_SESSION['producer_auth'];

    $redirector = $_COOKIE['redirector'];
    if (is_null($redirector))
        $redirector = $redirect;

    header("Location: " . $redirector);
    exit;
}
else
{
    session_start();
    //echo 'SESSION kteam: ' . $_SESSION['kteam_auth'] . ' producer:' . $_SESSION['producer_auth'];
    echo 'Login failed!!';
    header("Location: login.html");
}

?>
